﻿Imports System.Text
Imports Runa.Core
Imports Runa.UI.Effects

Public Class FormMain
    Private m_autoCompleteCollection As New AutoCompleteStringCollection

    Private Sub FormMain_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        For i As Double = 1 To 0 Step -0.1
            Me.Opacity = i
            Threading.Thread.Sleep(100)
        Next
    End Sub

    Private Sub FormMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim asm = GetType(Runa.UI.Effects.WpfEffectBase).Assembly
        Dim typs = asm.GetTypes()
        Me.LB_Effects.Items.Clear()
        For Each t In typs
            If t.Name.Contains("Effect") And t.Name.Contains("EffectBase") = False And t.FullName.Contains("Effects.Image") Then
                Me.LB_Effects.Items.Add(t.Name)
                m_autoCompleteCollection.Add(t.Name)
            End If
        Next
        Me.TB_Keyword.AutoCompleteCustomSource = m_autoCompleteCollection
    End Sub

    Private Sub TB_Keyword_TextChanged(sender As Object, e As System.EventArgs) Handles TB_Keyword.TextChanged
        If Me.m_autoCompleteCollection.Contains(Me.TB_Keyword.Text) Then
            Me.LB_Effects.SelectedItem = Me.TB_Keyword.Text
        End If
    End Sub

    Private Sub Btn_Clear_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Clear.Click
        Me.TB_Keyword.Clear()
    End Sub

    Private Sub Btn_Copy_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Copy.Click
        My.Computer.Clipboard.SetText(Me.TB_Result.Text)
    End Sub

    Private Sub Btn_Add_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Add.Click
        If Me.LB_Effects.SelectedItems.Count > 0 Then
            Call AddDeclation(Me.LB_Effects.SelectedItem.ToString())
        End If
    End Sub

    Private Sub Btn_AddAll_Click(sender As System.Object, e As System.EventArgs) Handles Btn_AddAll.Click
        Me.TB_Result.Clear()
        For Each item As String In Me.m_autoCompleteCollection
            Call Me.AddDeclation(item)
        Next
    End Sub

    Private Sub LB_Effects_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles LB_Effects.MouseDoubleClick
        Dim listBox = CType(sender, ListBox)
        For i As Integer = 0 To listBox.Items.Count - 1
            Dim r = listBox.GetItemRectangle(i)
            If r.Contains(e.Location) Then
                Dim item = listBox.Items(i).ToString()
                Call Me.AddDeclation(item)
            End If
        Next
    End Sub

    Private Sub AddDeclation(ByVal effectName As String)
        If Me.TB_Result.Text.Contains(effectName) = False Then
            Dim dec = String.Format("ThisGameParser.ImageEffects.Add('{0}', {1}.new(ThisGameParser))", effectName.GetLeft("Effect"), effectName)
            Dim sb As New StringBuilder(Me.TB_Result.Text)
            sb.AppendLine(dec)
            Me.TB_Result.Text = sb.ToString()
        End If
    End Sub

End Class